Ext.onReady(function() {
	
	var resourceModul = [
		{name: 'id'},
		{name: 'nodeId'},
		{name: 'menuName'},
		{name: 'parantNodeID'},
		{name: 'iconCls'},
		{name: 'urlPath'},
		{name: 'startmenu'},
		{name: 'menuOrder'},
		{name: 'isValiDate'},
		{name: 'description'},
		{name: 'type'},              
		{name: 'moduleId'},          
		{name: 'tooltip'},           
		{name: 'text'},              
		{name: 'title'},             
		{name: 'className'},         
		{name: 'launcherPaths'},     
		{name: 'shortcutIconCls'}
	];
	
	var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([
	    sm,
		new Ext.grid.RowNumberer(),
		{header:'tooltip', dataIndex:'tooltip', sortable:true},
		{header:'text', dataIndex:'text', sortable:true},
		{header:'title', dataIndex:'title', sortable:true},
		{header:'startmenu', dataIndex:'startmenu', sortable:true},
		{header:'描述', dataIndex:'description', sortable:true}
		
	]);
	var ds = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: '../basic/ResourcesAction/search.action'}),
		remoteSort: true,
		reader: new Ext.data.JsonReader(
			{
				totalProperty: 'total',
				idProperty:'id',
				root: 'invdata'
			},
			resourceModul
		)
	});
	
	ds.load({params: {start:0, limit:25}});
	
	var grid = new Ext.grid.GridPanel({
		ds: ds,
		cm: cm,
		sm: sm,
		viewConfig: {
			forceFit: true
		},
		bbar: new Ext.PagingToolbar({
			pageSize: 25,
			store: ds,
			displayInfo: true,
			displayMsg: '显示第{0}条到{1}条记录,一共{2}条',
			emptyMsg: '没有记录',
			items: [
		        '-',
		        {
		        	text: '快捷方式',
		        	iconCls: 'add',
		        	handler: function() {
		        		var records = grid.getSelectionModel().getSelections();
		        		saveShortcuts(records, '桌面快捷方式');
					}
		        },
		        {
		        	text: '快速启动',
		        	iconCls: 'add',
		        	handler: function() {
		        		var records = grid.getSelectionModel().getSelections();
						saveShortcuts(records, '快速启动');
					}
		        },
		        {
		        	text: '自动运行',
		        	iconCls: 'add',
		        	handler: function() {
		        		var records = grid.getSelectionModel().getSelections();
						saveShortcuts(records, '自动运行');
					}
		        }
			]
		})
	});
	
	function saveShortcuts(records, shortcutType) {
		var selRec = [];
		for(var i = 0; i < records.length; i++) {
			selRec.push(records[i].id);
		}
		var ss = Ext.util.JSON.encode(selRec);
		var oo = {};
		oo.resourceList = ss;
		oo.shortcutType = shortcutType;
		Ext.Ajax.request({
			method: 'post',
			url: '../basic/UserSettingAction/saveUserShortcut.action',
			params: oo,
			success:function(resp){
				var obj=Ext.util.JSON.decode(resp.responseText);
				if(obj.result == 'success') {
					Ext.MessageBox.alert('提示', '添加成功！');
					shortcutGridDS.load();
				}
				else {
					Ext.MessageBox.alert('提示', '添加失败！');
				}
			}
		});
	}
	
	var resourceWin = new Ext.Window({
		title: '所有可用资源列表',
		layout:'fit',
		width:680,
		height:300,
		modal : true,
		closeAction:'hide',
		plain: true,
		layout: 'fit',
		items: [grid]
	});
	
	
	var shortcutGridCM = new Ext.grid.ColumnModel([
   		new Ext.grid.RowNumberer(),
   		{header:'id', dataIndex:'id', sortable:true},
		{header:'userId', dataIndex:'userId', sortable:true},
		{header:'resourceId', dataIndex:'resourceId', sortable:true},
		{header:'shortcutType', dataIndex:'shortcutType', sortable:true}
   	]);
	
	var shortcutGridDS = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: '../basic/UserSettingAction/search.action'}),
		remoteSort: true,
		reader: new Ext.data.JsonReader(
			{
				totalProperty: 'total',
				idProperty:'id',
				root: 'invdata'
			},
			[
				{name: 'id'},
				{name: 'userId'},
				{name: 'resourceId'},
				{name: 'shortcutType'}
			]
		)
	});
	
	shortcutGridDS.load();
	
	var shortcutGrid = new Ext.grid.GridPanel({
		ds: shortcutGridDS,
		cm: shortcutGridCM,
		region: 'center',
		viewConfig: {
			forceFit: true
		},
		tbar: new Ext.Toolbar({
			buttonAlign: 'left',
			buttons: [
				{
					text: '添加'	,
					iconCls: 'add',
					handler: function() {
						resourceWin.show();
					}
				}
			]
		})
	});
	
	new Ext.Viewport({
		layout: 'border',
		items:[shortcutGrid]
	});
	
});